"""
文件数据加载器
"""
import pandas as pd
from typing import Dict, Any
from .base_loader import BaseLoader


class FileLoader(BaseLoader):
    """
    文件数据加载器
    """
    def load(self, source: str, **kwargs) -> Dict[str, Any]:
        """
        加载文件数据
        
        Args:
            source: 文件路径
            **kwargs: 加载参数
        """
        try:
            if source.endswith(".xlsx") or source.endswith(".xls"):
                data = pd.read_excel(source, **kwargs)
            elif source.endswith(".csv"):
                data = pd.read_csv(source, **kwargs)
            elif source.endswith(".json"):
                data = pd.read_json(source, **kwargs)
            else:
                raise ValueError(f"不支持的文件类型: {source}")
            
            return data.to_dict(orient="records")
        except FileNotFoundError as e:
            raise ValueError(f"文件不存在: {source}") from e
        except Exception as e:
            raise ValueError(f"加载文件失败: {str(e)}") from e